import Vue from 'vue'
import { Message,Loading} from 'element-ui';    
/* @desc:不拦截的请求名 */
const unInterceptorUrl = []

//不弹loading的请求名
const unLoadingUrl = ['upload-file']



/* @desc:检测请求名是否需要拦截 */
function checkUrl(resp){
    let arr = resp.config.url.split('/'),flag = true
    unInterceptorUrl.forEach(item=>{
        if(item === arr[arr.length-1]){
            flag = false
        }
    })
    return flag
}

/* @desc:检测不弹loading的请求名*/
function checkLoadUrl(resp){
    let arr = resp.url.split('/'),flag = true
    unLoadingUrl.forEach(item=>{
        if(item === arr[arr.length-1]){
            flag = false
        }
    })
    return flag
}



export const request = function(config){
    if(checkLoadUrl(config)){
        Loading.service({ fullscreen: true })
    }
    return config
}

export const response = function(response){
    Loading.service().close();
    if(response.data.code == 404){
        Message({
            showClose: true,
            message: '程序正在修复中'
        })
    }
    if(response.data.code!=200&&checkUrl(response)){
        Message({
            showClose: true,
            message: response.data.msg
        })
    }
    return response   
}